﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MvcMall.Core.Commands;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Core.Enums;
using MvcMall.Framework.Extensions;
using MvcMall.Framework.UnitOfWork;

namespace MvcMall.AdminWeb.Manage.GoodsBrands
{
    public partial class Save : ManageBasePage
    {
        private IGoodsBrandTask _goodsBrandTask;
        private IGoodsCategoryTask _goodsCatTask;

        protected void Page_Load(object sender, EventArgs e)
        {
            ValidatePagePower("GoodsBrandIndex");
            _goodsBrandTask = GetInstance<IGoodsBrandTask>();
            _goodsCatTask = GetInstance<IGoodsCategoryTask>();

            if (!IsPostBack)
            {

                ddlCats.DataSource = _goodsCatTask.GetByGrade(1).ToSelectList(c => c.Id.ToString(), c => c.CatName);
                ddlCats.DataBind();

                var id = 0;
                if (Int32.TryParse(Request.QueryString["Id"], out id))
                {
                    var info = _goodsBrandTask.Get(id);
                    if (info == null)
                    {
                        ShowMsgAndReturn("参数错误");
                        return;
                    }

                    lblId.Text = info.Id.ToString();
                    txtSortOrder.Text = info.SortOrder.ToString();
                    txtBrandName.Text = info.BrandName;
                    ddlChildCats.DataSource = info.GoodsCat.ParentCat.ChildCats.ToSelectList(c => c.Id.ToString(), c => c.CatName);
                    ddlChildCats.DataBind();
                    ddlChildCats.SelectedIndex =
                        ddlChildCats.Items.IndexOf(ddlChildCats.Items.FindByValue(info.GoodsCat.Id.ToString()));
                    ddlCats.SelectedIndex =
                        ddlCats.Items.IndexOf(ddlCats.Items.FindByValue(info.GoodsCat.ParentCat.Id.ToString()));

                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            var sortOrder = 0;
            if (string.IsNullOrEmpty(txtSortOrder.Text.Trim()) || string.IsNullOrEmpty(txtBrandName.Text.Trim()) || !Int32.TryParse(txtSortOrder.Text.Trim(), out sortOrder))
            {
                ShowMsg("输入不完整");
                return;
            }



            var brandName = txtBrandName.Text.Trim();

            var cmd = new SaveGoodsBrandCommand
                          {
                              BrandId = string.IsNullOrEmpty(lblId.Text) ? (int?)null : Convert.ToInt32(lblId.Text),
                              BrandName = brandName,
                              BrandStatus = GoodsBrandStatus.审核通过,
                              GoodsCatId = Convert.ToInt32(ddlChildCats.SelectedValue),
                              SortOrder = sortOrder
                          };
            using (var work = UnitOfWork.Begin())
            {
                if (_goodsBrandTask.Save(cmd) != null)
                {
                    work.Commit();
                    ShowMsg("保存成功", "Index.aspx");
                }
                else
                {
                    ShowMsg("保存失败");
                }
            }
        }

        protected void ddlCats_SelectedIndexChanged(object sender, EventArgs e)
        {
            var catInfo = _goodsCatTask.Get(Convert.ToInt32(ddlCats.SelectedValue));
            ddlChildCats.DataSource = catInfo.ChildCats.ToSelectList(c => c.Id.ToString(), c => c.CatName);
            ddlChildCats.DataBind();
        }
    }
}